package com.study.food;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.study.food.model.FoodIngredient;
import com.study.food.model.FoodMenu;
import com.study.util.FileUtil;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author wanghuilong
 * @version 1.0
 * @date 2022/6/25 4:34 下午
 */
public class FoodTest {

    public static final String menuFile = "/Users/wanghuilong/IdeaProjects/lazy/src/main/java/com/study/food/foodMenu.text";

    public static final String lastFile = "/Users/wanghuilong/IdeaProjects/lazy/src/main/java/com/study/food/last.txt";

    public static final Map<Integer,Integer> resultMap = new HashMap<>();

    public static final int itemNum = 2;

    static {
        resultMap.put(0,3);
        resultMap.put(1,3);
    }

    public static void main(String[] args) {
        String menuFileStr = FileUtil.read(menuFile);
        List<FoodMenu> foodMenuList = JSONObject.parseArray(menuFileStr, FoodMenu.class);
        String lastStr = FileUtil.read(lastFile);
        if(StringUtils.isNotEmpty(lastStr)) {
            lastStr = lastStr.trim().replace("\n","");
        }
        List<String> lastFoodNames = Lists.newArrayList(lastStr.split(","));

        Iterator<FoodMenu> iterator = foodMenuList.iterator();
        while (iterator.hasNext()) {
            FoodMenu foodMenu = iterator.next();
            if(lastFoodNames.contains(foodMenu.getName())) {
                iterator.remove();
            }
        }
        List<FoodMenu> resultList = new ArrayList<>();
        for (Map.Entry<Integer,Integer> entry:resultMap.entrySet()) {
            Integer foodType = entry.getKey();
            Integer num = entry.getValue();
            List<FoodMenu> menuList = foodMenuList.stream().filter(foodMenu -> foodMenu.getFoodType().equals(foodType))
                    .collect(Collectors.toList());
            for (int i = 0; i < num; i++) {
                int index = RandomUtils.nextInt(0, menuList.size() - 1);
                FoodMenu foodMenu = menuList.get(index);
                while (resultList.contains(foodMenu)) {
                    index = RandomUtils.nextInt(0, menuList.size() - 1);
                    foodMenu = menuList.get(index);
                }
                resultList.add(foodMenu);
            }
        }
        List<String> foodNames = new ArrayList<>();
        Map<String,Integer> foodIngredientMap = new HashMap<>();

        for (FoodMenu foodMenu:resultList) {
            foodNames.add(foodMenu.getName());
            List<FoodIngredient> ingredientList = foodMenu.getIngredientList();
            for (FoodIngredient foodIngredient:ingredientList) {
                Integer weight = foodIngredientMap.get(foodIngredient.getIngredientName());
                if(null == weight) {
                    weight = 0;
                }
                weight = (weight + foodIngredient.getWeight() * itemNum);
                foodIngredientMap.put(foodIngredient.getIngredientName(),weight);
            }
        }
        System.out.println(StringUtils.join(foodNames,","));
        for (Map.Entry<String,Integer> entry:foodIngredientMap.entrySet()) {
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
    }

}
